package com.incar.chuxing.baiduMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MarkerOptions.MarkerAnimateType;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.incar.chuxing.R;
import com.incar.chuxing.bean.MapParksAndCarsBean;
import com.incar.chuxing.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael on 11/6.
 */

public class MarkerUtils {
    private Context mContext;
    private BaiduMap mBaiduMap;
    private Bitmap parkBitmap;
    private List<Overlay> markerList;

    public MarkerUtils(BaiduMap mBaiduMap, Context mContext) {
        this.mContext = mContext;
        this.mBaiduMap = mBaiduMap;
    }


    /**
     * 添加网点和汽车
     */
    public void addParksMarker(List<MapParksAndCarsBean.AttachmentBean.ParksBean> parksBeanList,
                               List<MapParksAndCarsBean.AttachmentBean.CarModelsBean> carsBeanList) {
        List<OverlayOptions> optionsList = new ArrayList<>();
        if (parksBeanList != null) {
            for (int i = 0; i < parksBeanList.size(); i++) {
                //创建park图标
                parkBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.marker_park).copy(Bitmap.Config.ARGB_8888, true);
                Canvas canvas = new Canvas(parkBitmap);
                TextPaint textPaint = new TextPaint();
                textPaint.setAntiAlias(true);
                textPaint.setTextAlign(Paint.Align.CENTER);
                textPaint.setTextSize(DensityUtils.sp2px(mContext, 15f));
                textPaint.setColor(mContext.getResources().getColor(R.color.white));
                canvas.drawText(parksBeanList.get(i).getAvailableCar() + "", (float) 0.5 * parkBitmap.getWidth(), (float) 0.5 * parkBitmap.getHeight(), textPaint);// 设置bitmap上面的文字位置
                Bundle bundle = new Bundle();
                bundle.putInt("carOrPark", 0); //标识marker是网点还是汽车
                bundle.putString("parkId", parksBeanList.get(i).getId() + "");
                bundle.putString("parkName", parksBeanList.get(i).getLocationName());

                //设置marker
                OverlayOptions option = new MarkerOptions()
                        .position(new LatLng(parksBeanList.get(i).getLatitude(), parksBeanList.get(i).getLongitude()))
                        .icon(BitmapDescriptorFactory.fromBitmap(parkBitmap))
                        .animateType(MarkerAnimateType.grow)
                        .zIndex(2)
                        .period(800)
                        .extraInfo(bundle);
                optionsList.add(option);
                parkBitmap.recycle();
            }
        }
        if (carsBeanList != null) {
            for (int i = 0; i < carsBeanList.size(); i++) {
                Bundle bundle = new Bundle();
                bundle.putInt("carOrPark", 1); //标识marker是网点还是汽车 0网点  1汽车
                bundle.putInt("carMarkerId", i); //标识marker的序号，用于点击后获取相应的数据
                bundle.putString("timeStr", carsBeanList.get(i).getTimeStr()); //停放时长描述
                //设置marker
                OverlayOptions option = new MarkerOptions()
                        .position(new LatLng(carsBeanList.get(i).getLatitude(), carsBeanList.get(i).getLongitude()))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_car))
                        .animateType(MarkerAnimateType.grow)
                        .zIndex(2)
                        .period(800)
                        .extraInfo(bundle);
                optionsList.add(option);
            }
        }
        markerList = mBaiduMap.addOverlays(optionsList);
    }


    /**
     * 添加一个汽车marker
     *
     * @param longitude
     * @param latitude
     */
    public void addOneCar(double latitude, double longitude) {
        Overlay marker = mBaiduMap
                .addOverlay(new MarkerOptions()
                        .position(new LatLng(latitude, longitude))
                        .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.marker_car))));
        markerList.add(marker);
    }


    public void showParkInfoWindow(int layoutId, String content, final LatLng markerPosition) {
        //初始化marker的InfoWindow
        View infoWindow = LayoutInflater.from(mContext).inflate(layoutId, null);
        TextView infoParkDuration = (TextView) infoWindow.findViewById(R.id.info_park_duration);
        // ImageView infoNavi = (ImageView) infoWindow.findViewById(R.id.info_navi);
        infoParkDuration.setText(content);//车辆在网点停放时间
       /* infoNavi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//开始导航
                new WalkNavi((Activity) mContext).startWalkNavi(curPosition, markerPosition);
            }
        });*/
        InfoWindow mInfoWindow = new InfoWindow(infoWindow, markerPosition, -145);
        mBaiduMap.showInfoWindow(mInfoWindow);

    }

    public void hideInfoWindow() {
        mBaiduMap.hideInfoWindow();
    }


    /**
     * 清除地图上网点和车辆的Marker
     */
    public void clearParksAndCarsMarker() {
        if (markerList != null && markerList.size() != 0) {
            for (int i = 0; i < markerList.size(); i++) {
                markerList.get(i).remove();
            }
            markerList.clear();
        }
    }

}
